package com.ivc.info.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cs.framework.common.bo.Message;
import com.cs.framework.ui.bo.UIData;
import com.cs.framework.ui.web.UI;
import com.cs.framework.web.annotation.Action;
import com.cs.framework.web.annotation.Mapping;
import com.cs.framework.web.handler.Web;
import com.ivc.info.dto.ProductQueryDto;
import com.ivc.info.dto.base.ProductDto;
import com.ivc.info.service.ProductService;

/**
 * 商品
 * 
 * @author hedongzhou
 * 
 */
@Action("product")
public class ProductAction {

	private Log logger = LogFactory.getLog(getClass());

	@Resource
	private ProductService productService;

	/**
	 * 进入商品管理页面
	 */
	@Mapping
	public void mainPage() {
		UI.returnPage("info_pdt");
	}

	/**
	 * 查询商品
	 */
	@Mapping
	public Object query() {
		ProductQueryDto productQueryDto = Web.bind(ProductQueryDto.class);

		List<ProductQueryDto> list = productService.query(productQueryDto);
		int count = productService.count(productQueryDto);

		return new UIData(list, count);
	}

	/**
	 * 新增商品
	 */
	@Mapping
	public Object save() {
		ProductDto productDto = Web.bind(ProductDto.class);

		try {
			productService.saveAndStock(productDto);

			return new Message("Y", "新增成功");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);

			return new Message("N", e.getMessage());
		}
	}

	/**
	 * 修改商品
	 */
	@Mapping
	public Object update() {
		ProductDto productDto = Web.bind(ProductDto.class);

		try {
			productService.update(productDto);

			return new Message("Y", "修改成功");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);

			return new Message("N", e.getMessage());
		}
	}

}
